import Vue from 'vue';
import Yjlib from 'yj-rent-common-lib';
// import Yjlib from '/Users/yangning/npm-tools/rent-common-lib/index.js';
import YjComponent from 'yj-rent-common-component';
// import YjComponent from '/Users/yangning/npm-tools/rent-common-component/packages/index.js';

require("../common/css/common.scss");
const config = require('../../package');
import { showFullScreenLoading, tryHideFullScreenLoading } from "@/common/js/loading";
import { showMessage } from "@/common/js/message";
Vue.use(YjComponent); // 注册组件库-ui
import readOnly from './onlyReadForm'
import tooltipMap from '../common/js/tooltipMap.js'

Vue.nextTick(() => {
    readOnly.init()
})
window.devHostMap = {
    'http://sit-rent-business.block-os.com': 'http://localhost:10619',
    'https://sit-rent-business.block-os.com': 'http://localhost:10619'
}


function tooltipHandle (el, binding, vnode) {
  // 屏幕宽度
  let screenWidth = document.body ? document.body.clientWidth : document.documentElement.clientWidth
  // 屏幕宽度
  let screenHeight = document.body ? document.body.clientHeight : document.documentElement.clientHeight

  // 鼠标移入时，将浮沉元素插入到body中
  el.onmouseenter = function (e) {
    // 创建浮层元素并设置样式
    const vcTooltipDom = document.createElement("div");
    vcTooltipDom.style.cssText = `
            position: absolute;
            background: rgba(0, 0, 0, 0.7);
            color: #fff;
            font-size: 14px;
            line-height: 26px;
            text-align: justify;
            z-index: 9000;
            border-radius: 5px;
            padding: 10px 15px;
            max-width: 400px;
      `;
    // 设置id方便寻找
    vcTooltipDom.setAttribute("id", "vc-tooltip");
    // console.log('value', binding.value)
    let value = binding.modifiers.content ? binding.value : tooltipMap[binding.value];
    value = value.replace('/\r\n/g', '<br/>')
    value = value.replace('/\n/g', '<br/>')
    // 浮层中的文字 通过属性值传递动态的显示文案
    vcTooltipDom.innerHTML = value
    // 将浮层插入到body中
    if (value) {
      document.body.appendChild(vcTooltipDom);
    }
  };

  // 鼠标移动时，动态修改浮沉的位置属性
  el.onmousemove = function (e) {
    const vcTooltipDom = document.getElementById("vc-tooltip");

    if (vcTooltipDom) {
      let vcTooltipWidth = vcTooltipDom.clientWidth
      let vcTooltipHeight = vcTooltipDom.clientHeight
      

      // 下边缘设置
      if (screenHeight - e.clientY - 30 < vcTooltipHeight) {
        vcTooltipDom.style.top = e.clientY - vcTooltipHeight - 15 + "px";
      } else {
        vcTooltipDom.style.top = e.clientY + 15 + "px";
      }

      // 如果位置在中心位置左边
      if (e.clientX <= screenWidth / 2) {
        vcTooltipDom.style.left = e.clientX + 15 + "px";
      } else {
        // 右边
        vcTooltipDom.style.left = e.clientX - vcTooltipWidth - 15 + "px";
      }
    }
  };

  // 鼠标移出时将浮层元素销毁
  el.onmouseleave = function () {
    // 找到浮层元素并移出
    const vcTooltipDom = document.getElementById("vc-tooltip");
    if (vcTooltipDom) {
      document.body.removeChild(vcTooltipDom);
    }
  };
}

// 文字提示指令
Vue.directive('tooltip', {
    bind (el, binding, vnode) {
      tooltipHandle(el, binding, vnode)
    },
    update (el, binding, vnode) {
      tooltipHandle(el, binding, vnode)
    },
})

// 常量
const constants = {
    CODE_INTERVAL_SECONDS: 60,
    PAGE_SIZE_RANGE: [10, 20, 50, 100]
}

const baseConfig = {
    systemName: config.description,
    systemVersion: config.version,
    constants: constants
}


const yj = new Yjlib(baseConfig).init();

// 注册全局filter
if (yj.globalFilter) {
    yj.globalFilter.forEach(f => {
        Vue.filter(f.name, f.filter)
    })
}
// 权限校验
Vue.prototype.$verifyPerms = yj.authority.verifyPerms
// 权限别名
Vue.prototype.$menuName = yj.authority.menuName
// 权限menuId
Vue.prototype.$menuId = yj.authority.menuId
// 权限menuId
Vue.prototype.$menuRedirectBaseUrl = yj.authority.menuRedirectBaseUrl

// base
const ajaxConfig = {
    onError: (response) => {
        if (response) {
            const { data, status, config } = response
            const errorMsg = data && data.message ? data.message : '网络异常';
            console.log(data)
            if (!config.hiddenMessage) {
                showMessage(errorMsg, 'error')
            }
            if (status >= 600 && status < 700) {
                if (self != top) {
                    top.location.replace('/login/')
                } else {
                    location.replace('/login/')
                }
            }
        }
    },
    onLoading: (showLoading) => {
        if (showLoading) {
            showFullScreenLoading()
        } else {
            tryHideFullScreenLoading()
        }
    },
    baseUrl: '/server-api',
    deleteParamsFormBody: true,
    timeout: 60 * 1000,
    uploadUrl: '/v1/frameworks/photos/upload',
    uploadFileUrl: '/v1/frameworks/files/upload',
}

// 装载通讯模块
const ajaxRequest = new yj.AjaxRequest(ajaxConfig);
window.ajaxBase = ajaxRequest;


// 权限中心
const authConfig = {
    ...ajaxConfig,
    baseUrl: '/auth-api'
}
const authRequest = new yj.AjaxRequest(authConfig);
window.ajaxAuth = authRequest;


// 资产管理
const businessAssets = {
    ...ajaxConfig,
    baseUrl: '/commercialrent',
}
window.ajaxbusinessAssets = new yj.AjaxRequest(businessAssets);

// 合同管理
const businessContract = {
    ...ajaxConfig,
    baseUrl: '/commercialcontract',
}
window.ajaxBusinessContract = new yj.AjaxRequest(businessContract);

// 计划
const financialplan = {
    ...ajaxConfig,
    baseUrl: '/financialplan',
}

window.ajaxFinancialplan = new yj.AjaxRequest(financialplan);

// 流程相关
const flowApi = {
  ...ajaxConfig,
  baseUrl: '/api',
}

window.ajaxFlowApi = new yj.AjaxRequest(flowApi);